/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.support.android.designlibdemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.support.android.designlibdemo.CheeseListFragment;
import com.support.android.designlibdemo.Decoration.TestDecoration;
import com.support.android.designlibdemo.R;
import com.support.android.designlibdemo.RatioImageView;
import com.support.android.designlibdemo.TestData;
import com.support.android.designlibdemo.fragment.CartoonListFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * TODO
 */
public class MainSexActivity extends ToorBarActivity {

    private DrawerLayout mDrawerLayout;
    //   private RecyclerView mRecyclerView;
    //  private GridLayoutManager mGridManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);
        }
        //  setupAdapter();
        initFragment();
    }

    private void initFragment() {
        Fragment mFragment = CartoonListFragment.getInstanse();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_layout, mFragment).commit();
    }
//    private void setupAdapter() {
//        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
//
//        mGridManager = new GridLayoutManager(this, 2);
//        mGridManager.setSmoothScrollbarEnabled(true);
//        mGridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//            @Override
//            public int getSpanSize(int position) {
////                return position % 2 == 1 ? mGridManager.getSpanCount() : 1;
//                return 1;
//            }
//        });
//        mRecyclerView.addItemDecoration(new TestDecoration());
//        mRecyclerView.setLayoutManager(mGridManager);
//        mRecyclerView.setAdapter(new testAdapter(this));
//
//    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sample_actions, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        menuItem.setChecked(true);
                        mDrawerLayout.closeDrawers();
                        return true;
                    }
                });
    }

    @Override
    int setContentLayout() {
        return R.layout.activity_main_sex;
    }

    /* static class Adapter extends FragmentPagerAdapter {
         private final List<Fragment> mFragments = new ArrayList<>();
         private final List<String> mFragmentTitles = new ArrayList<>();

         public Adapter(FragmentManager fm) {
             super(fm);
         }

         public void addFragment(Fragment fragment, String title) {
             mFragments.add(fragment);
             mFragmentTitles.add(title);
         }

         @Override
         public Fragment getItem(int position) {
             return mFragments.get(position);
         }

         @Override
         public int getCount() {
             return mFragments.size();
         }

         @Override
         public CharSequence getPageTitle(int position) {
             return mFragmentTitles.get(position);
         }
     }*/
    static class Holder extends RecyclerView.ViewHolder {
        RatioImageView image;

        public Holder(View itemView) {
            super(itemView);
            image = (RatioImageView) itemView.findViewById(R.id.image);
            image.setOriginalSize(50, 50);
        }
    }

    static class testAdapter extends RecyclerView.Adapter<Holder> {
        Context activity;
        private List<String> listData;

        public testAdapter(Context activity) {
            this.activity = activity;
            listData = new ArrayList<String>();
            listData.addAll(Arrays.asList(TestData.imageThumbUrls));
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

            return new Holder(LayoutInflater.from(activity).inflate(R.layout.layout_sex_item, parent, false));
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            Glide.with(activity)
                    .load(listData.get(position))
                    .centerCrop().placeholder(R.mipmap.ic_launcher)
                    .into(holder.image);
            // .load("http://img.my.csdn.net/uploads/201407/26/1406383291_6518.jpg")
            //  Picasso.with(activity).load(listData.get(position)).into(holder.image);


        }

        @Override
        public int getItemCount() {
            return listData.size();
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }
    }
}
